import axios from "axios";

import { Toast } from "antd-mobile";

const env_url = process.env.REACT_APP_REQUEST_URL;
// 默认拦截射箭
axios.defaults.timeout = 100000;
// 本地地址
axios.defaults.baseURL = env_url;
/**
 * http request 拦截器
 */
axios.interceptors.request.use(
  (config) => {
    // 获取config 中的数据
    config.data = JSON.stringify(config.data);

    // 头信息对象
    let header = {};
    header["Content-Type"] = "application/json";
    // 鉴权 判断开头是不是以 /user 开头 并且不是login和registered 功能
    if (
      config.url.startsWith("/user") &&
      !config.url.startsWith("/user/login") &&
      !config.url.startsWith("/user/registered")
    ) {
      // 添加 授权的token
      header.authorization = localStorage.getItem("xyzf_user_token");
    }

    // if (config.url.startsWith("/houses/image")) {
    //   header["Content-Type"] = "multipart/form-data";
    // }
    config.headers = header;
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

/**
 * http response 拦截器
 */
axios.interceptors.response.use(
  (response) => {
    if (response.data && response.data.status === 400) {
      localStorage.removeItem("xyzf_user_token");
    }
    return response;
  },
  (error) => {
    Toast.fail("网络异常,请检查网络", 5, () => {}, true);
  }
);
/**
 * 封装get方法
 * @param url  请求url
 * @param params  请求参数
 * @returns {Promise}
 */
export function get(url, params = {}, headers = {}) {
  return new Promise((resolve, reject) => {
    axios
      .get(url, {
        params: params,
      })
      .then((response) => {
        resolve(response);
      })
      .catch((error) => {
        reject(error);
      });
  });
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, data) {
  return new Promise((resolve, reject) => {
    axios.post(url, data).then(
      (response) => {
        resolve(response);
      },
      (err) => {
        reject(err);
      }
    );
  });
}
/**
 * 封装delete请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function _delete(url, data) {
  return new Promise((resolve, reject) => {
    axios.delete(url, data).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        reject(err);
      }
    );
  });
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function patch(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.patch(url, data).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        msag(err);
        reject(err);
      }
    );
  });
}
/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.put(url, data).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        msag(err);
        reject(err);
      }
    );
  });
}
//失败提示
function msag(err) {
  if (err && err.response) {
    switch (err.response.status) {
      case 400:
        alert(err.response.data.error.details);
        break;
      case 401:
        alert("未授权，请登录");
        break;

      case 403:
        alert("拒绝访问");
        break;

      case 404:
        alert("请求地址出错");
        break;

      case 408:
        alert("请求超时");
        break;

      case 500:
        alert("服务器内部错误");
        break;

      case 501:
        alert("服务未实现");
        break;

      case 502:
        alert("网关错误");
        break;

      case 503:
        alert("服务不可用");
        break;

      case 504:
        alert("网关超时");
        break;

      case 505:
        alert("HTTP版本不受支持");
        break;
      default:
    }
  }
}
